<template>
	<view>
		<navbar name="腰围"></navbar>
		<view class="weui-flex">
			<view>
				<view class="placeholder titleView">
					腰围
				</view>
			</view>
			<view class="weui-flex__item">
				<view class="placeholder toolView" :class="label3 == '请选择腰围'?'':'colorA4'"
					@click="handleTap('picker3')">
					{{label3}}
				</view>
			</view>
		</view>
		<u-line />
		<view class="weui-flex">
			<view>
				<view class="placeholder titleView">
					选择时间
				</view>
			</view>
			<view class="weui-flex__item">
				<view class="placeholder toolView" :class="value1==''?'':'colorA4'" @click="handleTap('picker1')">
					{{value1==''?label:value1.replace(/-/g,".")}}
				</view>
			</view>
		</view>
		<u-line />

		<button class="relevance-btn" type="default" :disabled="disabled||label3 == '请选择腰围'?true:false"
			:class="disabled||label3 == '请选择腰围'?'disabledbg':''" @tap="addRecord(value3,value1)">保存</button>
		<!-- end -->
		<!-- 日期选择 -->
		<lb-picker ref="picker1" v-model="value1" :format="curFormat" display-format="YYYY-MM-DD-HH-mm-ss" mode="dateSelector"  @change="handleChange" @confirm="handleConfirm"
			@cancel="handleCancel" start-date="2021-1-1" :end-date="endTime">
		</lb-picker>
		<!-- end -->
		<lb-picker ref="picker3" v-model="value3" mode="unlinkedSelector" :list="list3" @change="handleChange"
			@confirm="handleConfirm3" @cancel="handleCancel">
		</lb-picker>
		<!-- end -->
	</view>
</template>

<script>
	import dayjs from '@/common/utils/day/day.js'
	import lbPicker  from '@/pages/subpackage/components/lb-picker/index';
	import uLine from "@/uview-ui/components/u-line/u-line.vue"
	export default {
		components: {
			lbPicker,
			uLine
		},
		data() {
			return {
				valueExpressionType:0,
				listLength: {},
				curFormat: 'YYYY-MM-DD HH:mm:ss',
				disabled: false,

				endTime: dayjs().format('YYYY-MM-DD HH:mm:ss'),
				label: '请选择时间',
				value1:'',

				indicId: '', //指标id
				label3: '请选择腰围',
				value3: [
					80,
				],
				list3: [
				],
			}
		},
		onLoad(e) {
			this.indicId = e.indicId ? e.indicId : 7;

			let gender = uni.getStorageSync(global.IS_DEV?'USER_DATA_BSY_T':'USER_DATA_BSY_Z').gender;
			this.getRangeIndicator()

		},
		methods: {
			//测量指标范围列表
			getRangeIndicator() {
				this.$api.get(global.apiUrls.getRangeIndicator, {})
					.then(res => {
						let result = res.data.result,
							range,
							type;
						for (let i = 0; i < result.length; i++) {
							if ('WAIST' == result[i].itemCode) { //腰围
								range = result[i].valueExpression.range;
								type = result[i].valueExpression.type
							}
						}
						this.valueExpressionType = type;
						this.$basejs.funFor(range, this, 'list3','cm',this.valueExpressionType)
						// let num1 = parseFloat(range.split('-')[0].replace('(', '')),
						// 	num2 = parseFloat(range.split('-')[1]);
						// this.listFun(num1, num2)
						// this.listFun(xsList[0], xsList[1], xsList[2], xsList[3])
						console.log(range, 12222)

					})
			},
			//身高
			handleConfirm3(e) {
				this.label3 = this.$basejs.itemFor(this, 'list3', e.item, true,this.valueExpressionType)
			},
			getLastTimeG() {
				let _this = this;
				this.$loginl.get(global.apiUrls.getUserInfoWidth, {})
					.then(res => {
						if (res.data.code == 1000) { //一天数据
							// console.log(res.data.result.height,'152222222222')

							if (res.data.result.height) {
								let height = res.data.result.height * 1 //
								_this.value3 = [
									'',
									'',
									height,
									'cm',
									'',
									''
								]
								_this.label3 = res.data.result.height
							}
							if (res.data.result.weight) {
								let weight = res.data.result.weight.toString();
								if(weight.split('.')){//有小数点
									weight = weight.split('.');
									_this.value2 = [
										'',
										'',
										parseInt(weight[0]),
										parseInt(weight[1]),
										'kg',
										'',
										''
									]
								}else{//没小数点
									_this.value2 = [
										'',
										'',
										parseInt(weight),
										0,
										'kg',
										'',
										''
									]
								}

								_this.label2 = res.data.result.weight
							}

						}
					})
			},
			// 记录腰围
			addRecord(value3, time) {
				this.disabled = true;
				let healthData = {
					value: this.$basejs.itemFor(this, 'list3', value3, false,this.valueExpressionType)
				}
				let _this = this,
					fdata = {
						deviceType: 'WAIST',
						recordTime: dayjs(time).format('YYYY-MM-DD'),
						createTime:time,
						inputType: 1, //1手动 2设备
						userId: uni.getStorageSync(global.IS_DEV?'USER_DATA_BSY_T':'USER_DATA_BSY_Z').userId,
						mac: '', //设备mac
						deviceId: '', //设备id
						healthDataId: '',
						healthData: JSON.stringify(healthData),
					};
				this.$api.post(global.apiUrls.addRecord, fdata).then(res => {
					if (res.data.code == 1000) {
						_this.value1 = ''
						uni.showToast({
							title: res.data.message,
							duration: 2000
						});
						setTimeout(() => {
							uni.navigateBack({
								delta: 1
							});
						}, 2000);
					} else {
						uni.showToast({
							title: res.data.message,
							icon: "none",
							duration: 2000
						});
					}
					_this.disabled = false;
				}).catch(err => {
					_this.disabled = false;
				})
			},
			handleTap(name) {
				this.$refs[name].show()
			},
			handleChange(e) {
				console.log('change::', e)
			},
			handleConfirm(e) {
				console.log(e)
			},
			handleCancel(e) {
				console.log('cancel::', e)
			},
		}
	}
</script>

<style lang="scss">
	@import "./record.scss"
</style>
